//package com.mezz.bones.framework.mybatis.config;
//
//import lombok.extern.slf4j.Slf4j;
//import org.flywaydb.core.Flyway;
//import org.flywaydb.core.api.FlywayException;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.boot.ApplicationArguments;
//import org.springframework.boot.ApplicationRunner;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import javax.sql.DataSource;
//
///**
// * Flyway 数据库迁移配置类
// * 使用 ApplicationRunner 在应用启动后初始化数据。
// */
//@Component
//@Slf4j
//public class FlywayMigrationRunner implements ApplicationRunner {
//
//  @Value("${spring.application.name}")
//  private String applicationName;
//
//  @Resource
//  private DataSource dataSource;
//
//  @Value("${flyway.enabled:false}")
//  private boolean flywayEnabled;  // 配置是否启用Flyway
//
//
//  @Override
//  public void run(ApplicationArguments args) {
//
//    if (!flywayEnabled) {
//      log.info("Skipping Flyway migration for application: {}", applicationName);
//      return;
//    }
//
//    Flyway flyway = Flyway.configure()
//            .outOfOrder(true)// 允许迁移脚本的版本号不按顺序执行
//            .dataSource(dataSource)
//            .locations("db/migration/" + applicationName)
//            .encoding("UTF-8")
//            .baselineOnMigrate(true)
//            .cleanDisabled(true)
//            .load();
//
//    try {
//      flyway.migrate();
//      log.info("Flyway migration completed successfully for application: {}", applicationName);
//    } catch (FlywayException e) {
//      handleFlywayException(flyway, e);
//    }
//  }
//
//  /**
//   * 处理 Flyway 异常
//   */
//  private void handleFlywayException(Flyway flyway, FlywayException e) {
//    log.error("Flyway migration failed for application: {}. Attempting repair...", applicationName, e);
//    try {
//      flyway.repair();
//      log.info("Flyway repair completed successfully for application: {}", applicationName);
//    } catch (FlywayException repairException) {
//      log.error("Flyway repair also failed for application: {}", applicationName, repairException);
//    }
//  }
//}
